package physics;

import build.Simulation;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

/**
 * Packages masses and connectors for their
 * addition to the work space. Contains little
 * behavior, but useful when looking at environment
 * forces.
 * @author Sam Rang
 *
 */
public class Assembly {
    private List<Mass> myMasses;
    private List<Connector> myConnectors;
    /**
     * Initializes the lists for masses and connectors.
     */
    public Assembly () {
        myMasses = new ArrayList<Mass>();
        myConnectors = new ArrayList<Connector>();
    }

    /**
     * Paint all shapes on the canvas.
     * @param pen graphics being painted
     */
    public void paint (Graphics2D pen) {
        for (Connector s : myConnectors) {
            s.paint(pen);
        }
        for (Mass m : myMasses) {
            m.paint(pen);
        }
    }

    /**
     * Called by Simulation, multiple times per second.
     * This should update the state of the animated shapes by just
     * a little so they appear to move over time.
     * @param dt The time since the last update
     * @param canvas The simulation containing the assembly
     */
    public void update (Simulation canvas, double dt) {
        for (Connector s : myConnectors) {
            s.update(canvas, dt);
        }
        for (Mass m : myMasses) {
            m.update(canvas, dt, this);
        }
    }

    /**
     * Searches the assembly for particular instance
     * of mass.
     * @param m Mass being searched for.
     * @return
     */
    public boolean contains (Mass m) {
        return myMasses.contains(m);
    }

    /**
     * This adds the Masses to the board.
     * @param mass The mass to be added
     */
    public void add (Mass mass) {
        myMasses.add(mass);
    }

    /**
     * This adds the Connectors to the board.
     * @param connector The Connector to be added
     */
    public void add (Connector connector) {
        myConnectors.add(connector);
    }

    /**
     * Removes particular instance of mass.
     * @param m Mass being removed
     */
    public void remove (Mass m) {
        myMasses.remove(m);
    }

    /**
     * Removes particular instance of connector.
     * @param c Connector being removed
     */
    public void remove (Connector c) {
        myConnectors.remove(c);
    }

    /**
     * Returns the masses contained in the assembly.
     * @return
     */
    public List<Mass> getMasses () {
        return myMasses;
    }

    /**
     * Returns the connectors contained in the assembly.
     * @return
     */
    public List<Connector> getConnectors () {
        return myConnectors;
    }
}
